package com.mk.carcassonne.drawables;

import com.mk.carcassonne.Global;
import com.mk.carcassonne.GlobalData;
import com.mk.carcassonne.Position;
import com.mk.carcassonne.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;

public class CommandBar implements Drawable{

	private Brick _nextBrick;
	private int _drawableId;
	
	public CommandBar(int drawableId)
	{
		_drawableId = drawableId;
	}
	
	@Override
	public void draw(Context context, Canvas canvas, Position shift) {
		
		Bitmap bgImage = Global.BitmapRepository.getBitmapForResource(_drawableId);
		Matrix bgImageMatrix = new Matrix();
		bgImageMatrix.setTranslate(0, Global.SCREEN_SIZE_Y - Global.COMMAND_BAR_HEIGHT);
		canvas.drawBitmap(bgImage, bgImageMatrix, null);
		
		if(_nextBrick != null)
		{
			Matrix brickImageMatrix = new Matrix();
			brickImageMatrix.postRotate(_nextBrick.getRotation() * 90, Global.BRICK_SIZE / 2, Global.BRICK_SIZE / 2);
			brickImageMatrix.postTranslate(Global.SCREEN_SIZE_X / 2 - Global.BRICK_SIZE / 2, Global.SCREEN_SIZE_Y - Global.BRICK_SIZE - 10);
			canvas.drawBitmap(_nextBrick.getBitmap(), brickImageMatrix, null);
			
			Matrix rotateIconMatrix = new Matrix();
			Bitmap rotateImage = Global.BitmapRepository.getBitmapForResource(R.drawable.rotate);
			rotateIconMatrix.setTranslate(getRotateButtonPosition().getX(), getRotateButtonPosition().getY());
			canvas.drawBitmap(rotateImage, rotateIconMatrix, null);
		}
		
		for(int i = 0; i < GlobalData.getRemainingFollowers(1); i++)
		{
			Bitmap followerImage = Global.BitmapRepository.getBitmapForResource(R.drawable.follower_red);
			Matrix followerImageMatrix = new Matrix();
			followerImageMatrix.setTranslate( (i + 1) * (Global.FOLLOWER_SIZE + 2), Global.SCREEN_SIZE_Y - Global.COMMAND_BAR_HEIGHT / 2 - Global.FOLLOWER_SIZE / 2);
			canvas.drawBitmap(followerImage, followerImageMatrix, null);			
		}
	}
	
	public void setNextBrick(Brick _nextBrick) {
		this._nextBrick = _nextBrick;
	}
	
	public Position getRotateButtonPosition()
	{
		return new Position(Global.SCREEN_SIZE_X / 2 - Global.BRICK_SIZE / 2 + Global.BRICK_SIZE + 10, Global.SCREEN_SIZE_Y - Global.BRICK_SIZE - 10);
	}
}
